// subpages0/liverPage/liverPage.js
var app = getApp();
import { request } from "../../utils/request.js"
let livePlayer;
Page({
  data: {
    showLoading: true,
    noMoretip: false,
    page: 0, 
    slideList: [],
    liveList: [],
    liveListRight: [],
    liveListLeft: [],
    showBackBtn: true,
  },
  onLoad: function (e) {
    var that = this;
    app.calNavBarHeight(that); //计算得出导航栏高度
    var pathArr = getCurrentPages();
    if (pathArr.length <= 1) {
      this.setData({
        showBackBtn: false
      })
    }
    if (!app.globalData.plumSession) {
      //回调
      app.plumSessionCallback = plumSession => {
        if (plumSession != '') {
          console.log('查询到登录信息');
          this.requestliveConfig();
          this.requestLiverList();
        } else {
          console.log('未查询到登录信息');
          app.wechatSq(that);
        }
      }
    } else {
      this.requestliveConfig();
      this.requestLiverList();
    }
  },
  //返回上一页
  backLastPage(e) {
    let type = e.currentTarget.dataset.type;
    if (type == 'last') {
      wx.navigateBack({
        delta: 1
      })
    } else {
      app.backHome()
    }
  },
  requestliveConfig: function () {
    var that = this;
    wx.request({
      url: app.globalData.liverqurl,
      data: {
        map: 'applet_appletlive_carousel'
      },
      success: function (res) {
        console.log(res.data.data)
        if (res.data.ec == 200) {
          that.setData({
            slideList: res.data.data
          })
        } else {
        }
      },
      complete: function () {
        
      }
    });
  },
  requestLiverList: function () {
    var that = this;
    var page = that.data.page;
    var searchVal = that.data.searchVal;
    wx.request({
      url: app.globalData.liverqurl,
      data: {
        map: 'applet_appletlive_room_list',
        page: page,
      },
      success: function (res) {
        console.log(res.data.data)
        if (res.data.ec == 200) {
          livePlayer = requirePlugin('live-player-plugin');
          let newList = res.data.data, liveListLeft = that.data.liveListLeft, liveListRight = that.data.liveListRight;
          for (let i = 0; i < newList.length; i++) {
            //查询直播间状态
            livePlayer.getLiveStatus({ room_id: newList[i].room_id }).then(res => { 
              if (newList[i].live_status != res.liveStatus) {
                newList[i].live_status = res.liveStatus;
                app.updateLiveStatus(newList[i].room_id, res.liveStatus)
              }
            }).catch(err => {
              console.log(err)
            })
            if (i % 2 == 0) {
              liveListLeft.push(newList[i])
            } else {
              liveListRight.push(newList[i])
            }
          }
          if (page == 0) {
            var allList = newList;
          } else {
            var allList = that.data.liveList.concat(newList)
          }

          that.setData({
            liveList: allList,
            showLoading: false,
            liveListLeft: liveListLeft,
            liveListRight: liveListRight
          })
          console.log(that.data.liveList)
          if (newList.length < 10) {
            that.setData({
              noMoretip: true
            })
          }
        } else {
          console.log(res.data)
          that.setData({
            noMoretip: true,
            showLoading: false
          });
        }
      },
      complete: function () {
        wx.stopPullDownRefresh();
      }
    });
  },
  toLiverPrediction: function (e) {
    var roomId = e.currentTarget.dataset.id;
    var title = e.currentTarget.dataset.title;
    console.log(roomId);
    wx.navigateTo({
      url: '/subpages0/liverPrediction/liverPrediction?roomId=' + roomId + '&title=' + title,
    })
  },
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      page: 0,
      noMoretip: false,
      showLoading: true,
      liveList: [],
      liveListRight: [],
      liveListLeft: [],
    });
    that.requestLiverList();
    console.log("下拉刷新");
  },
  // onReachBottom: function () {
  //   var that = this;
  //   console.log("到达页面底部")
  //   var isMore = that.data.noMoretip;
  //   var page = that.data.page;
  //   page++;
  //   that.setData({
  //     page: page,
  //   })
  //   if (isMore) {
  //     console.log("已完成或正在加载");
  //   } else {
  //     that.requestLiverList();
  //   }
  // },
})